package dianmobile.eistarandroid.background;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import dianmobile.eistarandroid.ByhhAndroidApplication;
import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.network.request.MyNewMailsRequest;
import dianmobile.eistarandroid.network.request.RequestCallback;
import dianmobile.eistarandroid.ui.activities.MainActivity;
import dianmobile.eistarandroid.utils.ConstantsData;
import dianmobile.eistarandroid.utils.SettingInfoManager;

public class GetMyActivityService extends Service {

    private long timeInterVal;
    public GetMyActivityService() {
    }

    @Override
    public void onCreate() {
        int time = new SettingInfoManager(this).getTimeInterval();
        switch (time) {
            case 0:
                timeInterVal = 30000;
                break;
            case 1:
                timeInterVal = 300000;
                break;
            case 2:
                timeInterVal = 900000;
                break;
            case 3:
                timeInterVal = 1800000;
                break;
            default:
                break;
        }
    }
    @Override
    public int onStartCommand(Intent intent, int flag, int startId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                TimerTask task = new TimerTask() {
                    @Override
                    public void run() {
                        Log.d("service", "send request");
                        MyNewMailsRequest.execute(GetMyActivityService.this, new RequestCallback() {
                            @Override
                            public void onResult(Map<String, Object> result, String error) {
                                if (error == null && result.get(ConstantsData.RESULT_DATA) != null) {
                                    boolean hasNewMails = (boolean)result.get(ConstantsData.RESULT_DATA);
                                    if (hasNewMails) {
                                        sendNotification("NEW_MAILS", "您有新的信件");
                                    }
                                }
                            }
                        });
                    }
                };
                Timer timer = new Timer();
                timer.schedule(task, 0, timeInterVal);
            }
        }).start();
        return super.onStartCommand(intent, flag, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private void sendNotification(String type, String notificationMsg) {
//        Notification notification = new Notification(R.mipmap.icon, "您有新的消息", System.currentTimeMillis());

        //TODO 根据type跳到不同的fragment

        Intent notificationIntent = new Intent(this, MainActivity.class);
        ByhhAndroidApplication application = (ByhhAndroidApplication)getApplication();
        notificationIntent.putExtra(ConstantsData.EXTRA_USER_NAME, application.getLoginUserName());
        switch (type) {
            case "NEWS":
                notificationIntent.putExtra("type", ConstantsData.MENU_MY_ACTIVITIES);
                break;
            case "NEW_MAILS":
                notificationIntent.putExtra("type", ConstantsData.MENU_MY_MAILS);
                break;
            default:
                break;
        }
//        Intent notificationIntent = new Intent("android.setting.SETTINGS");
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        Notification.Builder builder = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.icon)
                .setContentTitle("白云黄鹤BBS")
                .setContentText(notificationMsg)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .setOngoing(true);
        Notification notification = builder.getNotification();

        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.flags = Notification.FLAG_AUTO_CANCEL;

//        notification.setLatestEventInfo(this, "白云黄鹤BBS", "您有"+notificationNum+"条未读消息", pendingIntent);

        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, notification);
    }
}
